<?php
namespace Swiftx\Common;
use Swiftx\System\Iterator;
/**
 * 框架分页对象
 * @author      胡永强 <odaytudio@gmail.com>
 * @since       2015-12-15
 * @copyright   Copyright (c) 2014-2015 Swiftx Inc.
 * @property int Current 当前页页码
 * @property int Number 每页显示记录数
 * @property-read int Count 总记录数
 * @property-read int Total 总页数
 */
class Page extends Iterator {

	/** @var int 当前页页码 */
	protected $_current;
	/** @var int 每页显示记录数 */
	protected $_number;
	/** @var int 总记录数 */
	protected $_count = null;
	/** @var callable 统计回调 */
	protected $_callCount;
	/** @var callable 加载回调 */
	protected $_callData;

	/**
	 * 获取该模型所有实例
	 * @param int $current
	 * @param int $number
	 * @param callable $callCount
	 * @param callable $callData
	 */
	public function __construct($current, $number,callable $callCount,callable $callData){
		$this->_current = $current;
		$this->_number = $number;
		$this->_callCount = $callCount;
		$this->_callData = $callData;
		$this->_data = call_user_func($callData, $current, $number);
	}

	/**
	 * 获取当前页页码
	 * @return int
	 */
	public function _getCurrent(){
		return $this->_current;
	}

	/**
	 * 获取每页显示记录数
	 * @return int
	 */
	public function _getNumber(){
		return $this->_number;
	}

	/**
	 * 获取总记录数
	 * @return int
	 */
	public function _getCount(){
		if($this->_count!=null) return $this->_count;
		$this->_count = call_user_func($this->_callCount);
		return $this->_count;
	}

	/**
	 * 获取总页数
	 * @return int
	 */
	public function _getTotal(){
		$Total = intval($this->Count/$this->Number);
		if($this->Count%$this->Number > 0) $Total += 1;
		return $Total;
	}

}